package com.example.shixun1.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;

import com.example.shixun1.R;

public class SettingsActivity extends AppCompatActivity {
    private SwitchCompat switchAdminMode;
    private LinearLayout layoutAddArticle, layoutAddProduct;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        // 初始化视图
        initViews();

        // 设置监听器
        setupListeners();

        // 初始化状态
        initAdminState();
    }

    private void initViews() {
        switchAdminMode = findViewById(R.id.switch_admin_mode);
        layoutAddArticle = findViewById(R.id.layout_add_article);
        layoutAddProduct = findViewById(R.id.layout_add_product);
    }

    private void setupListeners() {
        // 账号管理
        findViewById(R.id.layout_account_manage).setOnClickListener(v -> {
            startActivity(new Intent(this, AccountManageActivity.class));
        });

        // 退出登录
        findViewById(R.id.layout_logout).setOnClickListener(v -> {
            showLogoutConfirmationDialog();
        });

        // 管理员模式切换
        switchAdminMode.setOnCheckedChangeListener((buttonView, isChecked) -> {
            updateAdminUIVisibility(isChecked);
            saveAdminMode(isChecked);
        });

        // 添加文章
        layoutAddArticle.setOnClickListener(v -> {
            startActivity(new Intent(this, AddArticleActivity.class));
        });

        // 添加商品
        layoutAddProduct.setOnClickListener(v -> {
            startActivity(new Intent(this, AddProductActivity.class));
        });
    }

    private void initAdminState() {
        boolean isAdmin = getSharedPreferences("app_prefs", MODE_PRIVATE)
                .getBoolean("admin_mode", false);
        switchAdminMode.setChecked(isAdmin);
        updateAdminUIVisibility(isAdmin);
    }

    private void showLogoutConfirmationDialog() {
        new AlertDialog.Builder(this)
                .setTitle("确认退出")
                .setMessage("确定要退出当前账号吗？")
                .setPositiveButton("确定", (dialog, which) -> performLogout())
                .setNegativeButton("取消", null)
                .show();
    }

    private void performLogout() {
        // 清除所有登录相关状态
        getSharedPreferences("user_prefs", MODE_PRIVATE).edit().clear().apply();
        getSharedPreferences("login_state", MODE_PRIVATE).edit().clear().apply();
        getSharedPreferences("app_prefs", MODE_PRIVATE).edit().clear().apply();

        // 跳转到登录页并清除返回栈
        Intent intent = new Intent(this, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }

    private void updateAdminUIVisibility(boolean isAdmin) {
        layoutAddArticle.setVisibility(isAdmin ? View.VISIBLE : View.GONE);
        layoutAddProduct.setVisibility(isAdmin ? View.VISIBLE : View.GONE);
    }

    private void saveAdminMode(boolean isAdmin) {
        getSharedPreferences("app_prefs", MODE_PRIVATE).edit()
                .putBoolean("admin_mode", isAdmin)
                .apply();
    }
}